package com.bourne.autoDB;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.bourne.enums.BizCodeEnum;
import com.bourne.exception.BizException;
import com.bourne.util.AutoCreationData;
import com.bourne.util.JsonData;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

/**
 * @author bourne
 * @description 字码自动生成工具
 * @PROJECT_NAME: base-service
 * @DATE: 2021/3/21 19:20
 */
@Configuration
@Component
public class MyBatisPlusGenerator {

    public static JsonData autoCreation(AutoCreationData autoCreationData) {
        try {
            GlobalConfig config = new GlobalConfig();
            config.setActiveRecord(true)
                    .setOutputDir(autoCreationData.getSavePath())
                    .setFileOverride(true)
                    .setIdType(IdType.ASSIGN_UUID)
                    .setDateType(DateType.ONLY_DATE)
                    .setServiceName("%sService")
                    .setEntityName("%sDO")
                    .setBaseResultMap(true)
                    .setActiveRecord(false)
                    .setBaseColumnList(true);
            String author = autoCreationData.getAuthor();
            if (StringUtils.isNotBlank(author)) {
                config.setAuthor(autoCreationData.getAuthor());
            }
            DataSourceConfig dsConfig = new DataSourceConfig();
            dsConfig.setDbType(DbType.MYSQL)
                    .setDriverName(autoCreationData.getDriverName())
                    .setUrl(autoCreationData.getDbUrl())
                    .setUsername(autoCreationData.getDbLoginName())
                    .setPassword(autoCreationData.getDbLoginPassWord());
            StrategyConfig stConfig = new StrategyConfig();
            stConfig.setCapitalMode(true)
                    .setNaming(NamingStrategy.underline_to_camel)
                    .setEntityLombokModel(true)
                    .setRestControllerStyle(true)
                    .setInclude(autoCreationData.getTables());
            PackageConfig pkConfig = new PackageConfig();
            pkConfig.setParent(autoCreationData.getFileParent())
                    .setMapper("mapper")
                    .setService("service")
                    .setController("controller")
                    .setEntity("model")
                    .setXml("mapper");
            AutoGenerator ag = new AutoGenerator();
            ag.setGlobalConfig(config)
                    .setDataSource(dsConfig)
                    .setStrategy(stConfig)
                    .setPackageInfo(pkConfig);
            ag.execute();
            return JsonData.buildSuccess();
        } catch (Exception e) {
            throw new BizException(BizCodeEnum.CREATE_DB_DO_FAIL);
        }
    }

    public static void main(String[] args) {
        AutoCreationData autoCreationData = AutoCreationData.builder()
                .author("bourne")
                .tables("usesr")
                .dbLoginName("root")
                .dbLoginPassWord("Sxy_78031989")
                .driverName("com.p6spy.engine.spy.P6SpyDriver")
                .dbUrl("jdbc:p6spy:mysql://localhost:3306/base_core?serverTimezone=Asia/Shanghai")
                .fileParent("com.bourne")
                .savePath("D:\\\\prk\\\\mybatis").build();
        MyBatisPlusGenerator.autoCreation(autoCreationData);
    }

}
